<template>
  <div
    class='container'
    @scroll="remPos"
    ref="articeBox"
  >
    <van-nav-bar
      fixed
      title="文章详情"
      left-arrow
      @click-left="$router.back()"
    />
    <!-- 文章详细内容 -->
    <div class="detail">
      <h3 class="title">{{articleDetail.title}}</h3>
      <div class="author">
        <van-image
          round
          width="1rem"
          height="1rem"
          fit="fill"
          :src="articleDetail.aut_photo"
        />
        <div class="text">
          <p class="name">{{articleDetail.aut_name}}</p>
          <p class="time">{{articleDetail.pubdate|relTime}}</p>
        </div>
        <van-button
          round
          size="small"
          type="info"
          @click="changeFollow"
        >{{articleDetail.is_followed?'已关注':'+ 关注'}}</van-button>
      </div>
      <div class="content">
        <p v-html="articleDetail.content"></p>
      </div>
      <div class="zan">
        <van-button
          round
          size="small"
          :class="{active:articleDetail.attitude===1}"
          plain
          icon="like-o"
          @click="toggleLike(1)"
        >点赞</van-button>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button
          round
          size="small"
          :class="{active:articleDetail.attitude===0}"
          plain
          icon="delete"
          @click="toggleLike(0)"
        >不喜欢</van-button>
      </div>
      <!-- 评论组件 -->
      <CommentView />
    </div>

  </div>
</template>

<script>
import { like, unLike, disLike, unDisLike, getDetailById } from '@/api/news'
import { follow, unFollow } from '@/api/user'
import CommentView from './components/comment'
export default {
  name: 'article-index',
  data () {
    return {
      articleDetail: {}, // 文章详情
      scrollTop: 0// 记录滚动距离
    }
  },
  methods: {
    async getDetail () { // 获取文章详情
      const { data } = await getDetailById(this.$route.params.id)
      this.articleDetail = data
      // console.log(data)
    },
    remPos (e) { // 记录滚动位置
      this.scrollTop = e.target.scrollTop
    },
    async changeFollow () { // 关注/取关
      // eslint-disable-next-line camelcase
      const { aut_id, is_followed } = this.articleDetail
      // eslint-disable-next-line camelcase
      if (is_followed) {
        // is_followed 为true 证明 已经关注了  要取关
        await unFollow(aut_id)
        // 前台
        this.articleDetail.is_followed = false
        this.$toast.success('已取关')
      } else {
        // is_followed false 证明 没有关注  要关注
        await follow(aut_id)
        this.articleDetail.is_followed = true
        this.$toast.success('已关注')
      }
    },
    async toggleLike (type) { // 切换喜欢/不喜欢文章
      try {
        // eslint-disable-next-line camelcase
        const { attitude, art_id } = this.articleDetail
        if (type === 1) {
          if (attitude === 1) { // 如果当前为1  证明要取消为无态度-1
            // console.log(this.$route.params.id.toString())
            await unLike(art_id)
            this.articleDetail.attitude = -1
            this.$toast.success('已取消点赞')
          } else {
            await like(art_id)
            this.articleDetail.attitude = 1
            this.$toast.success('点赞成功')
          }
        } else {
          if (attitude === 0) { // 如果当前为0  证明要取消不喜欢
            await unDisLike(art_id)
            this.articleDetail.attitude = -1
            this.$toast.success('已移除')
          } else {
            await disLike(art_id)
            this.articleDetail.attitude = 0
            this.$toast.success('已加入')
          }
        }
      } catch (error) {
        this.$toast.fail('操作失败')
      }
    }
  },
  activated () { // 钩子函数 组件被缓存后 只要进入就触发此函数
    // 如果 本次进入的文章id跟 传过来的id不一致 证明 用户进入了另一篇文章  这时候就调用接口 获取新的文章
    // 否则 就证明 用户又返回上次浏览的文章。这时候就要跳转到上次用户浏览到的地方==》阅读记忆
    if (this.articleDetail.art_id?.toString() !== this.$route.params.id) {
      this.scrollTop = 0
      this.getDetail()
    } else {
      this.$refs.articeBox.scrollTop = this.scrollTop
    }
  },
  components: {
    CommentView
  }
}
</script>

<style scoped lang='less'>
.container {
  height: 100%;
  overflow-y: auto;
  box-sizing: border-box;
}
.detail {
  padding: 46px 10px 44px;
  height: 100%;
  .title {
    font-size: 18px;
    line-height: 2;
  }
  .zan {
    text-align: center;
    padding: 10px 0;
    .active {
      border-color: red;
      color: red;
    }
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.5;
      text-align: left;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  // 内容中包含：img 特别宽  code pre 不能换行
  .content {
    padding: 20px 0;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    /deep/ img {
      max-width: 100%;
      background: #f9f9f9;
    }
    /deep/ code {
      white-space: pre-wrap;
    }
    /deep/ pre {
      white-space: pre-wrap;
    }
  }
}
</style>
